
Imports cmplib01

Public Class ctlCampaignerAssets
    ' this control handles displayed items for a particular player, it is also where orders are set and such.
    Inherits System.Windows.Forms.UserControl

    Private Const c_Module As String = "ctlCampaignerAssets"

    Public Const c_NOPLAYER As Int16 = -1

    Private Enum AssetDisplayMode
        UnitsDisplay = 0
        ObjectivesDisplay = 1
        ResourcesDisplay = 2
    End Enum

    Public Event InstallationSelected(ByRef obj As InstallationChit)
    Public Event UnitSelected(ByRef unit As BattlegroupChit)
    Public Event TokenSelected(ByRef tok As TokenType)

    Private ptrCurrentGame As CampaignerGameData
    Private c_ENABLEDBACKCOLOR As Color = Color.SteelBlue
    Private c_SELECTEDOPTIONBACKCOLOR As Color = Color.PaleGoldenrod

    Private m_CurrentPlayer As Int16 = c_NOPLAYER
    Private m_DisplayMode As AssetDisplayMode = AssetDisplayMode.UnitsDisplay

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        DisplayList()
    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlClient As System.Windows.Forms.Panel
    Friend WithEvents lstItems As System.Windows.Forms.ListBox
    Friend WithEvents pnlSubPanel As System.Windows.Forms.Panel
    Friend WithEvents pnlTextPanel As System.Windows.Forms.Panel
    Friend WithEvents lblText As System.Windows.Forms.Label
    Friend WithEvents btnUnits As System.Windows.Forms.Button
    Friend WithEvents btnObjectives As System.Windows.Forms.Button
    Friend WithEvents btnResources As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pnlClient = New System.Windows.Forms.Panel()
        Me.lstItems = New System.Windows.Forms.ListBox()
        Me.pnlSubPanel = New System.Windows.Forms.Panel()
        Me.pnlTextPanel = New System.Windows.Forms.Panel()
        Me.lblText = New System.Windows.Forms.Label()
        Me.btnUnits = New System.Windows.Forms.Button()
        Me.btnObjectives = New System.Windows.Forms.Button()
        Me.btnResources = New System.Windows.Forms.Button()
        Me.pnlClient.SuspendLayout()
        Me.pnlTextPanel.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlClient
        '
        Me.pnlClient.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlClient.BackColor = System.Drawing.Color.LightSkyBlue
        Me.pnlClient.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlClient.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstItems, Me.pnlSubPanel, Me.pnlTextPanel, Me.btnUnits, Me.btnObjectives, Me.btnResources})
        Me.pnlClient.Name = "pnlClient"
        Me.pnlClient.Size = New System.Drawing.Size(210, 500)
        Me.pnlClient.TabIndex = 1
        '
        'lstItems
        '
        Me.lstItems.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.lstItems.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstItems.Location = New System.Drawing.Point(-1, 80)
        Me.lstItems.Name = "lstItems"
        Me.lstItems.Size = New System.Drawing.Size(210, 418)
        Me.lstItems.TabIndex = 1
        '
        'pnlSubPanel
        '
        Me.pnlSubPanel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlSubPanel.BackColor = System.Drawing.Color.PaleGoldenrod
        Me.pnlSubPanel.Location = New System.Drawing.Point(-1, 70)
        Me.pnlSubPanel.Name = "pnlSubPanel"
        Me.pnlSubPanel.Size = New System.Drawing.Size(210, 16)
        Me.pnlSubPanel.TabIndex = 5
        '
        'pnlTextPanel
        '
        Me.pnlTextPanel.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlTextPanel.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTextPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlTextPanel.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblText})
        Me.pnlTextPanel.Location = New System.Drawing.Point(-1, -5)
        Me.pnlTextPanel.Name = "pnlTextPanel"
        Me.pnlTextPanel.Size = New System.Drawing.Size(224, 48)
        Me.pnlTextPanel.TabIndex = 0
        '
        'lblText
        '
        Me.lblText.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblText.Location = New System.Drawing.Point(8, 16)
        Me.lblText.Name = "lblText"
        Me.lblText.Size = New System.Drawing.Size(152, 24)
        Me.lblText.TabIndex = 0
        Me.lblText.Text = "Assets"
        '
        'btnUnits
        '
        Me.btnUnits.BackColor = System.Drawing.Color.PaleGoldenrod
        Me.btnUnits.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnUnits.ForeColor = System.Drawing.Color.Black
        Me.btnUnits.Location = New System.Drawing.Point(-1, 48)
        Me.btnUnits.Name = "btnUnits"
        Me.btnUnits.Size = New System.Drawing.Size(68, 24)
        Me.btnUnits.TabIndex = 2
        Me.btnUnits.Text = "Units"
        '
        'btnObjectives
        '
        Me.btnObjectives.BackColor = System.Drawing.Color.SteelBlue
        Me.btnObjectives.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnObjectives.ForeColor = System.Drawing.Color.Black
        Me.btnObjectives.Location = New System.Drawing.Point(65, 48)
        Me.btnObjectives.Name = "btnObjectives"
        Me.btnObjectives.Size = New System.Drawing.Size(77, 24)
        Me.btnObjectives.TabIndex = 3
        Me.btnObjectives.Text = "Installations"
        '
        'btnResources
        '
        Me.btnResources.BackColor = System.Drawing.Color.SteelBlue
        Me.btnResources.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnResources.ForeColor = System.Drawing.Color.Black
        Me.btnResources.Location = New System.Drawing.Point(141, 48)
        Me.btnResources.Name = "btnResources"
        Me.btnResources.Size = New System.Drawing.Size(68, 24)
        Me.btnResources.TabIndex = 4
        Me.btnResources.Text = "Resources"
        '
        'ctlCampaignerAssets
        '
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pnlClient})
        Me.Name = "ctlCampaignerAssets"
        Me.Size = New System.Drawing.Size(210, 500)
        Me.pnlClient.ResumeLayout(False)
        Me.pnlTextPanel.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Overrides Sub Refresh()

        lstItems.Items.Clear()

        Select Case m_DisplayMode
            Case AssetDisplayMode.UnitsDisplay : DisplayPlayerUnits()
            Case AssetDisplayMode.ObjectivesDisplay : DisplayFactionObjectives()
            Case AssetDisplayMode.ResourcesDisplay : DisplayFactionResources()
        End Select

    End Sub

    Private Sub btnUnits_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnits.Click
        DisplayMode = AssetDisplayMode.UnitsDisplay
        Me.Refresh()
    End Sub

    Private Sub btnObjectives_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnObjectives.Click
        DisplayMode = AssetDisplayMode.ObjectivesDisplay
        Me.Refresh()
    End Sub

    Private Sub btnResources_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResources.Click
        DisplayMode = AssetDisplayMode.ResourcesDisplay
        Me.Refresh()
    End Sub

#Region "Display Actions"

    Private Sub DisplayList()
        ' displays the list of items for the currently selected player
        If m_CurrentPlayer = -1 Then
            ' disable all the main buttons
            MainButtonsEnabled = False
        Else
            MainButtonsEnabled = True
        End If

    End Sub

    Private Sub DisplayPlayerUnits()
        'Dim key As String
        'Dim gc As GroundChitType

        If ptrCurrentGame.selectedPlayer Is Nothing Then Exit Sub
        If ptrCurrentGame.selectedFaction Is Nothing Then Exit Sub

        For Each mem As ChitMember In ptrCurrentGame.Chits
            If mem.UnitType = GroundChitClass_cgd.Battlegroup AndAlso mem.OwningPlayer.Equals(ptrCurrentGame.selectedPlayer) Then
                lstItems.Items.Add(mem)
            End If
        Next
    End Sub

    Private Sub DisplayFactionObjectives()

        '       Dim key As String
        '        Dim gc As GroundChitType
        Dim fact As Faction

        If ptrCurrentGame.selectedPlayer Is Nothing Then Exit Sub
        If ptrCurrentGame.selectedFaction Is Nothing Then Exit Sub

        fact = ptrCurrentGame.Faction_g.Item(ptrCurrentGame.selectedPlayer.FactionKey)

        For Each mem As ChitMember In ptrCurrentGame.Chits
            If mem.UnitType = GroundChitClass_cgd.Objective AndAlso mem.OwningFaction.Equals(ptrCurrentGame.selectedFaction) Then
                lstItems.Items.Add(mem)
            End If
        Next

    End Sub

    Private Sub DisplayFactionResources()
        ' this should show the total of each type of resource owned by the faction, even if it is 0
        Dim key As String
        Dim ret As ResourceType
        'Dim objKey As String
        'Dim objResKey As String
        'Dim objRes As Resource
        Dim resourceAmount As Int32
        Dim addAmount As Int32
        Dim maxAmount As Int32
        Dim fact As Faction

        If ptrCurrentGame.selectedPlayer Is Nothing Then Exit Sub
        If ptrCurrentGame.selectedFaction Is Nothing Then Exit Sub

        Try
            fact = ptrCurrentGame.Faction_g.Item(ptrCurrentGame.selectedPlayer.FactionKey)

            For Each key In ptrCurrentGame.ResourceTypes_g.HT.Keys
                ret = ptrCurrentGame.ResourceTypes_g.Item(key)
                resourceAmount = 0
                addAmount = 0
                maxAmount = 0

                '// find all resources of this type
                For Each mem As ChitMember In ptrCurrentGame.Chits
                    If mem.UnitType = GroundChitClass_cgd.Objective AndAlso mem.OwningFaction.Equals(ptrCurrentGame.selectedFaction) Then
                        Dim install As InstallationChit = mem
                        For Each respool As ResourcePoolItem In install.Resources
                            If respool.resourceTypeptr.ResourceTypeID = ret.ResourceTypeID Then
                                resourceAmount += respool.localAmount
                                addAmount += respool.accrueAmount
                                maxAmount += respool.maxAmount
                            End If
                        Next
                    End If
                Next

                lstItems.Items.Add(CStr(ret.ResourceName & " [" & resourceAmount & "/" & maxAmount & "] +" & addAmount))

            Next

            'For Each key In ptrCurrentGame.ResourceTypes_g.HT.Keys
            '    ret = ptrCurrentGame.ResourceTypes_g.Item(key)
            '    resourceAmount = 0
            '    ' find all of the resources of this type current in each objective and add it to the total
            '    For Each objKey In ptrCurrentGame.AllChits.Keys
            '        gc = ptrCurrentGame.AllChits.Item(objKey)
            '        If gc.GroundChitClass = GroundChitClass_cgd.Objective Then
            '            obj = gc
            '            If obj.OwningFaction = fact.FactionID Then
            '                ' check the resource type owned by this objective
            '                For Each objResKey In obj.ObjectiveResources.HT.Keys
            '                    objRes = obj.ObjectiveResources.HT.Item(objResKey)
            '                    If objRes.ResourceTypeID = ret.ResourceTypeID Then
            '                        resourceAmount += objRes.CurrentAmount
            '                    End If
            '                Next
            '            End If
            '        End If
            '    Next

            'lstItems.Items.Add(ret.ResourceName & " [" & resourceAmount & "]")
            'Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

#End Region

#Region "Properties"

    Public Property MainButtonsEnabled() As Boolean
        Get
            Return btnUnits.Enabled
        End Get
        Set(ByVal Value As Boolean)
            btnUnits.Enabled = Value
            btnObjectives.Enabled = Value
            btnResources.Enabled = Value
            lstItems.Enabled = Value
            If Value = False Then
                btnUnits.BackColor = c_DISABLEDBUTTONBACKCOLOR
                btnObjectives.BackColor = c_DISABLEDBUTTONBACKCOLOR
                btnResources.BackColor = c_DISABLEDBUTTONBACKCOLOR
                btnUnits.ForeColor = c_DISABLEDBUTTONFONTCOLOR
                btnObjectives.ForeColor = c_DISABLEDBUTTONFONTCOLOR
                btnResources.ForeColor = c_DISABLEDBUTTONFONTCOLOR
                pnlSubPanel.BackColor = c_DISABLEDBUTTONBACKCOLOR
            Else
                btnUnits.BackColor = c_ENABLEDBACKCOLOR
                btnObjectives.BackColor = c_ENABLEDBACKCOLOR
                btnResources.BackColor = c_ENABLEDBACKCOLOR
                btnUnits.ForeColor = c_ENABLEDBUTTONFONTCOLOR
                btnObjectives.ForeColor = c_ENABLEDBUTTONFONTCOLOR
                btnResources.ForeColor = c_ENABLEDBUTTONFONTCOLOR
                pnlSubPanel.BackColor = c_SELECTEDOPTIONBACKCOLOR
                Select Case m_DisplayMode
                    Case AssetDisplayMode.UnitsDisplay
                        btnUnits.BackColor = c_SELECTEDOPTIONBACKCOLOR
                    Case AssetDisplayMode.ObjectivesDisplay
                        btnObjectives.BackColor = c_SELECTEDOPTIONBACKCOLOR
                    Case AssetDisplayMode.ResourcesDisplay
                        btnObjectives.BackColor = c_SELECTEDOPTIONBACKCOLOR
                End Select
            End If
        End Set
    End Property

    Public Property CurrentGame() As CampaignerGameData
        Get
            Return ptrCurrentGame
        End Get
        Set(ByVal Value As CampaignerGameData)
            ptrCurrentGame = Value
        End Set
    End Property

    Private Property DisplayMode() As AssetDisplayMode
        Get
            Return m_DisplayMode
        End Get
        Set(ByVal Value As AssetDisplayMode)
            m_DisplayMode = Value

            If MainButtonsEnabled = True Then
                ' set them all to unselected
                btnUnits.BackColor = c_ENABLEDBACKCOLOR
                btnObjectives.BackColor = c_ENABLEDBACKCOLOR
                btnResources.BackColor = c_ENABLEDBACKCOLOR
                Select Case m_DisplayMode
                    Case AssetDisplayMode.UnitsDisplay
                        btnUnits.BackColor = c_SELECTEDOPTIONBACKCOLOR
                    Case AssetDisplayMode.ObjectivesDisplay
                        btnObjectives.BackColor = c_SELECTEDOPTIONBACKCOLOR
                    Case AssetDisplayMode.ResourcesDisplay
                        btnResources.BackColor = c_SELECTEDOPTIONBACKCOLOR
                End Select
            End If

        End Set
    End Property

#End Region

    Private Sub lstItems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstItems.SelectedIndexChanged
        If lstItems.SelectedItem Is Nothing Then Exit Sub

        ' raise the item selected event.
        Select Case m_DisplayMode
            Case AssetDisplayMode.ObjectivesDisplay : RaiseEvent InstallationSelected(lstItems.SelectedItem)
            Case AssetDisplayMode.UnitsDisplay : RaiseEvent UnitSelected(lstItems.SelectedItem)
        End Select
    End Sub

End Class
